$(document).ready(function () {        
   $("#ok_button").click(function() {
       parent.close_dialog();
   });
});

var icons = [];

function keyEvent(e, qInput) {
    if (window.event) {
        e = window.event;
    }    
    
    var b = (window.event) ? window.event.keyCode : e.keyCode;
    
    
    if (b != 13 && b != 38 && b != 40 && b != 33 && b != 34) {        
        window.clearTimeout(t);            
        t = window.setTimeout("searchLocations('" + qInput.value + "');", 500);   
    }
    else {        
        ;
    }
}

var xr;
var t;

function searchLocations(qInputVal) {     
    var content = document.getElementById("search_results");            
    content.innerHTML = '<div style="text-align: center; padding-top: 30%;"><img src="/style/images/myfoon2/ajax-loading.gif" width="48" height="48"/></div>';
    var sll = gmap.getCenter();
    var url = "/locationSearch?q=" + encodeURIComponent(qInputVal) + "&sll=" + encodeURIComponent(sll);
    sendRequest(url, processAjaxCall);
}

xhr = function () {
    var a = null;

    if (window.XMLHttpRequest) {
        try  {
            a = new XMLHttpRequest();
        }
        catch (ex) {
        ;
        }
    }
    else if (window.ActiveXObject) {
        for (var i = 0, arr;arr = ["MSXML2.XMLHTTP.6.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"][i++];) {
            try  {
                a = new ActiveXObject(arr);
                break 
            }
            catch (ex) {
                ;
            }
        }
    }
    return a;
};

function sendRequest(url, handler) {
    xr = xhr();   
    xr.onreadystatechange = handler;    
    xr.open("GET", url, true);    
    xr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
    xr.send(null);
}

function processAjaxCall() {
    if (xr.readyState == 4) {
        if (xr.status == 200) {
            processLocationData(xr.responseText);
            //document.getElementById("search_results").innerHTML = xr.responseText;
        }
    }
}

function clearAllMarkers() {
    gmap.clearOverlays();    
}

var selIndex = -1;
function selectMarker(lat, lng, nr) {
   var i = 0;   
   for (i = 0; i < 5; i++) {
       if (i == nr) {
           document.getElementById("location" + i).className = "selected";           
           selIndex = nr;
       }
       else {
           document.getElementById("location" + i).className = "";           
       }
   }
   
   latlng = new GLatLng(lat, lng);       
   gmap.setCenter(latlng);
}

function createMarker(lat, lng, nr) {  
   latlng = new GLatLng(lat, lng);    
   //gmap.clearOverlays();
     
   gmap.addOverlay(new google.maps.Marker(latlng, icons[nr]));   
   gmap.setCenter(latlng, 15);
}                     

var locationData;

function doSelectLocation() {  
  parent.document.getElementById("td_map").innerHTML =  '<a class="apps" href="javascript: void(0);" onclick="alert(1);">' +
                                                        '<img src="/style/images/h/map-icon.png" border="0" width="16" height="16"/>' + 
                                                        '<span>' +
                                                        locationData.locations[selIndex].location_name + 
                                                        '</span>' + 
                                                        '</a>'; 
  parent.document.getElementById("modalPage").style.display = "none";
}

function processLocationData(data) {
    locationData = JSON.parse(data);  
    selIndex = -1;
    
    var content = document.getElementById("search_results");
    
    var ol = '<ol style="height: 220px; margin: 0; list-style: none; padding:0; font-size: 11px; font-family: Tahoma;">';
    var i = 0;
    var cmText;
    
    clearAllMarkers();
    var iter = 0;
    for (i in locationData.locations) {        
        cmText = 'selectMarker(' + locationData.locations[i].location_lat + ', ' + 
                                   locationData.locations[i].location_lng + ', ' + 
                                   iter + ');';
        
        ol += '<li id="location'+ iter + '">' +                       
              '<a href="javascript: void(0);" onclick="' + cmText + '">' + 
              '<img src="/style/images/markers/'+(iter+1)+'.png" width="16" height="16" border="0"/>' +
              '<span style="display: inline; padding-left: 5px;">' + locationData.locations[i].location_name + '</span>' +             
              '<span>' + locationData.locations[i].location_streetaddress + ' ' +
              '<b>'  + locationData.locations[i].location_city + '</b>' + ' ' + 
              '<b>'  + locationData.locations[i].location_country + '</b>' +                        
              '</span>' +               
              '</a>' +
              '</li>';        
        createMarker(locationData.locations[i].location_lat, locationData.locations[i].location_lng, iter);
        iter++;
    }
    ol += '</ol>';                        
    
    if (iter > 0) {
     content.innerHTML = ol;   
    }    
    else {
     content.innerHTML = '<span style="font-family: Tahoma; font-size: 11px; font-weight: bold; color: black;">Your search did not match any locations</span>' + 
                         '<div style="margin-top: 15px;"><span style="font-family: Tahoma; font-size: 11px; font-weight: bold; color: black;">Suggestions</span>' +
                         '<ul style="padding-left: 10px; padding-top: 0px; font-family: Tahoma; font-size: 11px; font-weight: normal; color: black;">'+
                         '<li>Make sure all street and city names are spelled correctly.</li>' + 
                         '<li>Make sure your search includes a city and state. (e.g. Bahcesehir University, Besiktas Istanbul)</li>' +
                         '<ul>'+
                         '</div>';
    }
}

function loadMaps() {
  google.load("maps", "2", {"callback" : mapsLoaded});
}

function initLoader() {
  var script = document.createElement("script");
  script.src = "http://www.google.com/jsapi?key=ABQIAAAA6PkGijZQl8bQ0xwCakOhKRRp0EPLZ06U0IjAPe16hUDFLTcm8RR0nciZV-g9Mj_rBY18jTmM_Yb87w&callback=loadMaps";
  script.type = "text/javascript";
  document.getElementsByTagName("head")[0].appendChild(script);  
}

var mapInitialized = false;


function mapsLoaded() {  
  gmap = new google.maps.Map2(document.getElementById("map_div"), {size:new GSize(325,275)});
  gmap.setCenter(new google.maps.LatLng(41.006961, 29.046580), 12);
  gmap.enableDoubleClickZoom(); 
  gmap.enableContinuousZoom();
  gmap.enableScrollWheelZoom();                    
  
  gmap.addControl(new google.maps.ScaleControl());
  gmap.addControl(new google.maps.LargeMapControl());
  gmap.addControl(new google.maps.MapTypeControl());

  createIcons();
  //mgr = new MarkerManager(gmap, {trackMarkers:true}); 
  /*
  google.maps.Event.addListener(gmap, 'click', function(overlay, latlng) {
          alert(latlng.lat() + '-' + latlng.lng());          
  });      
  
  google.maps.Event.addListener(gmap, 'moveend', function() {
          window.document.getElementById("sllInputHidden").value = gmap.getCenter();          
        });                         
  */  
  mapInitialized = true;
}

function initializeAll() {
    if (!mapInitialized) {
        initLoader();
        loadMaps();
    }    
    
    loc_searchOnBlur();
}


function createIcons() {
      var m1 = new GIcon();
  m1.image = "/style/images/markers/marker1.png";
  m1.shadow = "http://www.google.com/mapfiles/shadow50.png";
  m1.iconSize = new GSize(20, 34);
  m1.shadowSize = new GSize(37, 34);
  m1.iconAnchor = new GPoint(9, 34);
  m1.infoWindowAnchor = new GPoint(9, 2);
  m1.infoShadowAnchor = new GPoint(18, 25);
  m1.transparent = "http://www.google.com/intl/en_ALL/mapfiles/markerTransparent.png";
  
  var m2 = new GIcon();
  m2.image = "/style/images/markers/marker2.png";
  m2 .shadow = "http://www.google.com/mapfiles/shadow50.png";
  m2.iconSize = new GSize(20, 34);
  m2.shadowSize = new GSize(37, 34);
  m2.iconAnchor = new GPoint(9, 34);
  m2.infoWindowAnchor = new GPoint(9, 2);
  m2.infoShadowAnchor = new GPoint(18, 25);
  m2.transparent = "http://www.google.com/intl/en_ALL/mapfiles/markerTransparent.png";
  
  var m3 = new GIcon();
  m3.image = "/style/images/markers/marker3.png";
  m3.shadow = "http://www.google.com/mapfiles/shadow50.png";
  m3.iconSize = new GSize(20, 34);
  m3 .shadowSize = new GSize(37, 34);
  m3 .iconAnchor = new GPoint(9, 34);
  m3.infoWindowAnchor = new GPoint(9, 2);
  m3.infoShadowAnchor = new GPoint(18, 25);
  m3.transparent = "http://www.google.com/intl/en_ALL/mapfiles/markerTransparent.png";
  
  var m4 = new GIcon();
  m4.image = "/style/images/markers/marker4.png";
  m4 .shadow = "http://www.google.com/mapfiles/shadow50.png";
  m4 .iconSize = new GSize(20, 34);
  m4  .shadowSize = new GSize(37, 34);
  m4 .iconAnchor = new GPoint(9, 34);
  m4.infoWindowAnchor = new GPoint(9, 2);
  m4.infoShadowAnchor = new GPoint(18, 25);
  m4.transparent = "http://www.google.com/intl/en_ALL/mapfiles/markerTransparent.png";
  
  var m5 = new GIcon();
  m5.image = "/style/images/markers/marker5.png";
  m5.shadow = "http://www.google.com/mapfiles/shadow50.png";
  m5 .iconSize = new GSize(20, 34);
  m5  .shadowSize = new GSize(37, 34);
  m5  .iconAnchor = new GPoint(9, 34);
  m5.infoWindowAnchor = new GPoint(9, 2);
  m5.infoShadowAnchor = new GPoint(18, 25);
  m5.transparent = "http://www.google.com/intl/en_ALL/mapfiles/markerTransparent.png";
  
  var m6 = new GIcon();
  m6.image = "/style/images/markers/marker6.png";
  m6.shadow = "http://www.google.com/mapfiles/shadow50.png";
  m6.iconSize = new GSize(20, 34);
  m6 .shadowSize = new GSize(37, 34);
  m6 .iconAnchor = new GPoint(9, 34);
  m6.infoWindowAnchor = new GPoint(9, 2);
  m6.infoShadowAnchor = new GPoint(18, 25);
  m6.transparent = "http://www.google.com/intl/en_ALL/mapfiles/markerTransparent.png";
  
  icons[0] = m1;
  icons[1] = m2;
  icons[2] = m3;
  icons[3] = m4;
  icons[4] = m5;
  icons[5] = m6;
}